<template>
  <div class="app-container">
    <el-row :gutter="20">

      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="应用名称" prop="appName">
            <el-input v-model="queryParams.domain.appName" placeholder="应用名称" clearable size="small"
              style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="应用类型" prop="appType">             <el-select v-model="queryParams.domain.appType" placeholder="应用类型" clearable size="small"               style="width: 240px">
              <el-option v-for="dict in appTypeOpts" :key="dict.dictValue" :label="dict.dictLabel"                  :value="dict.dictValue" />
            </el-select>           </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" stripe border>
          <el-table-column label="应用类型" align="center" key="appTypeName" prop="appTypeName"
            :show-overflow-tooltip="true"  width="100px" />
          <el-table-column label="应用名称" width="140px" align="center" key="appName" prop="appName" :show-overflow-tooltip="true"  />
          <el-table-column label="文件名称" width="140px"  align="center" key="fileName" prop="fileName" :show-overflow-tooltip="true" />
          <el-table-column label="应用描述" align="center" key="appDes" prop="appDes" :show-overflow-tooltip="true" />
          <el-table-column label="备注" align="center" key="remark" prop="remark" :show-overflow-tooltip="true" />
          <el-table-column label="新增日期"  width="160px"align="center" key="createTime" prop="createTime"/>
          <el-table-column label="实例(运行中/总数)" align="center" key="allRun" width="120px">
            <template slot-scope="scope">
              <el-tooltip class="item" effect="dark" content="点击查看实例详情" placement="right-end">
                <el-button type="primary" size="mini" @click="toJarInst(scope.row)" round>{{scope.row.allRun}}
                </el-button>
              </el-tooltip>
            </template>
          </el-table-column>

          <el-table-column label="操作"  align="center" width="150" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="mini"   v-loading.lock= "loading" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
              <el-button size="mini" type="text" icon="el-icon-c-scale-to-original" @click="handleSet(scope.row)">配置</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用类型" prop="appType">
               <el-select v-model="form.appType" placeholder="请选择应用类型">
                 <el-option v-for="dict in appTypeOptsNew"
                   :key="dict.dictValue"
                   :label="dict.dictLabel"
                   :value="dict.dictValue" >
                 </el-option>
               </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用名称" prop="appName">
              <el-input v-model="form.appName" placeholder="应用名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="文件名称" prop="fileName">
              <el-input v-model="form.fileName" placeholder="文件名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用描述" prop="appDes">
              <el-input type="textarea" v-model="form.appDes" placeholder="应用描述" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="说明" prop="remark">
              <el-input type="textarea" v-model="form.remark" placeholder="请输入备注说明" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm"  v-loading.lock= "loading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改参数配置对话框 -->
    <el-dialog  :title="settingTitle" :visible.sync="settingOpen" width="800px" append-to-body>

      <el-form :model="setQuery" ref="setQuery" :inline="true"  label-width="68px">
        <el-form-item label="配置名称" prop="setName">
          <el-input v-model="setQuery.domain.setName" placeholder="配置名称" clearable size="small"
            style="width: 240px" @keyup.enter.native="getSettting" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getSettting">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table  v-loading="loading" :data="settingList" border fit highlight-current-row style="width: 100%">
        <el-table-column width="120px" align="center" label="配置名称">
          <template slot-scope="{row}">
            <span>{{ row.setName }}</span>
          </template>
        </el-table-column>


        <el-table-column min-width="300px" label="配置值">
          <template slot-scope="{row}">
            <template v-if="row.edit">
              <el-input v-model="row.setValue" class="edit-input" size="small" />
              </el-button>
            </template>
            <span v-else>{{ row.setValue }}</span>
          </template>
        </el-table-column>

       <el-table-column min-width="200px" label="配置描述">
         <template slot-scope="{row}">
           <template v-if="row.edit">
             <el-input v-model="row.setDes" class="edit-input" size="small" />
           </template>
           <span v-else>{{ row.setDes }}</span>
         </template>
       </el-table-column>

        <el-table-column align="center" label="操作" width="120">
          <template slot-scope="{row}">
            <span>
            <el-button v-if="row.edit" type="success" size="small" icon="el-icon-circle-check-outline"
              @click="confirmEdit(row)">
              完成
            </el-button>
            </span>
            <span>
            <el-button v-if="row.edit" type="cancel-btn" size="small" icon="el-icon-circle-check-outline"
              @click="cancelEdit(row)">
              取消
            </el-button>

            <el-button v-else type="primary" size="small" icon="el-icon-edit" @click="row.edit=!row.edit">
              编辑
            </el-button>
            </span>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="setTotal>0" :total="setTotal" :page.sync="setQuery.pageNum" :limit.sync="setQuery.pageSize"
        @pagination="getSettting" />
    </el-dialog>


  </div>
</template>

<script>
  import {
    listApp,
    addApp,
    updApp,
    delApp,
    listSetting,
    updSetting,
    getAppType
  } from "@/api/app";

  export default {
    name: "JarModel",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        setTotal:0,
        // 用户表格数据
        userList: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        settingOpen: false,
        settingTitle: "",
        settingList: [],
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "label"
        },
        appTypeOpts: [],
        isEdit: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          domain: {

          }
        },
        setQuery: {
          pageNum: 1,
          pageSize: 10,
          domain: {

          }
        },
        // 表单校验
        rules: {
          appName: [{
            required: true,
            message: "应用名称不能为空",
            trigger: "blur"
          }],
          appType: [{
            required: true,
            message: "应用类型不能为空",
            trigger: "blur"
          }]
        }
      };
    },
    watch: {},
    computed:{
      	appTypeOptsNew() {
      		return this.appTypeOpts.filter(function(data) {
      			return data.dictValue !== ''
      		})
      	}
    },
    created() {
      this.getAppTypes();
      this.getList();
    },
    methods: {

      getAppTypes() {
        let defaultOpt = {
          "dictValue": "",
          "dictLabel": "全部"
        };
        this.appTypeOpts.push(defaultOpt);
        getAppType().then(res => {
          let data = res.data;
          data.map(item => this.appTypeOpts.push({
            "dictValue": item.type,
            "dictLabel": item.name
          }));
        })
      },

      /** 查询列表 */
      getList() {
        this.loading = true;
        listApp(this.queryParams).then(response => {
          this.userList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }).catch((err) => {
          this.loading = false;
        });
      },

      /***查询配置*/
      getSettting(row) {
        listSetting( this.setQuery).then(response => {
          const items = response.data.records;
          this.settingList = items.map(v => {
            this.$set(v, 'edit', false)
            v.oriValue = v.setValue;
            v.oriSetDes = v.setDes;
            return v
          })
          this.loading = false;
          this.setTotal =  response.data.total;
        });

      },

      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "新增应用";
        this.form = {};
        this.isEdit = false;
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.open = true;
        this.title = "修改应用";
        this.form = JSON.parse(JSON.stringify(row ));
        this.isEdit = true;
      },
      /**配置*/
      handleSet(row) {
        this.loading = true;
        this.setQuery.domain ={};
        this.settingOpen = true;
        this.settingTitle = row.appName + "：配置管理";
        this.setQuery.domain.setType = 0;
        this.setQuery.domain.appId = row.id;
        this.getSettting(row);
      },
      /** 提交按钮 */
      submitForm: function() {
        this.loading = true;
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updApp(this.form).then(response => {
                this.loading = false;
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).catch(()=>{
                 this.loading = false;
              });;
            } else {
              addApp(this.form).then(response => {
                this.loading = false;
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).catch(()=>{
                 this.loading = false;
              });;
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.loading = true;
        const userIds = row.id;
        this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delApp(userIds);
        }).then(() => {
           this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(()=>{
           this.loading = false;
        })
      },
      //跳转到详情
      toJarInst(row) {
        this.$router.push({
          path: '/app/appInst',
          query: {
            appId: row.id
          }
        })
      },
      cancelEdit(row) {
        debugger
        row.setValue = row.oriValue;
        row.setDes = row.oriSetDes;
        row.edit = false;
      },
      confirmEdit(row) {
        row.edit = false
        row.oriValue = row.setValue
        row.oriSetDes = row.setDes;
        row.appId = row.relId;
        updSetting(row).then(res => {
          if (res.success) {
            this.$message({
              message: '编辑完成',
              type: 'success'
            })
          } else {
            this.$message({
              message: res.message,
              type: 'error'
            })
          }
        });
      }
    }
  };
</script>
